﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SpaceNeedle.x6473.WindowsPhone.ViewModels;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using System.IO;
using SpaceNeedle.x6473.WindowsPhone.Models;
using Newtonsoft.Json;
using System.Device.Location;
using Microsoft.Phone.Maps.Services;
using System.Text;
using SpaceNeedle.x6473.WindowsPhone.Common;
using Windows.Networking.Connectivity;
using System.Net.Http;
using SpaceNeedle.SDK.WindowsPhone.Views;
using System.Windows.Controls.Primitives;
using SpaceNeedle.SDK.WindowsPhone;

namespace SpaceNeedle.x6473.WindowsPhone.Views
{
    public class RequestItemDetailsView : ViewBase<RequestItemDetailsViewModel> { }
    public partial class RequestItemDetails : RequestItemDetailsView
    {
        public RequestItemDetails()
        {
            InitializeComponent();
            this.ViewModel.Loaded += ViewModel_Loaded;
            this.ViewModel.RequestItemSupportChanged += ViewModel_RequestItemSupportChanged;
        }

        void ViewModel_RequestItemSupportChanged()
        {
            this.SetSupporterButtonIcon();
            supportersCountTextBlock.Text = this.ViewModel.RequestItem.TotalSupportersCount.ToString();
        }

        private void SetSupporterButtonIcon()
        {
            int likeButtonIndex = 1;
            var button = ((ApplicationBarIconButton)this.ApplicationBar.Buttons[likeButtonIndex]);

            if (this.ViewModel.IsSupportingRequestItem)
            {
                button.IconUri = new Uri("/Assets/AppBar/minus.png", UriKind.Relative);
                button.Text = "deixar de apoiar";
            }
            else
            {
                button.IconUri = new Uri("/Assets/AppBar/add.png", UriKind.Relative);
                button.Text = "apoiar";
            }
        }
        async void ViewModel_Loaded()
        {
            this.SetSupporterButtonIcon();

            this.ViewModel.RequestItem.SupportersCount = await Api.GetRequestItemSupportersCount(AppViewModel.Instance.Properties.SelectedRequestItem.Id);

            supportersCountTextBlock.Text = this.ViewModel.RequestItem.TotalSupportersCount.ToString();

            if (AppViewModel.Instance.Properties.SelectedRequestItem.CreatedBy == AppViewModel.Instance.Properties.Me.Id.ToString())
            {
                protocolNumberStack.Visibility = Visibility.Visible;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string requestItemId = string.Empty;

            NavigationContext.QueryString.TryGetValue("requestItemId", out requestItemId);

            if (!string.IsNullOrEmpty(requestItemId))
            {
                AppViewModel.Instance.Properties.SelectedRequestItemId = requestItemId;
            }

            base.OnNavigatedTo(e);
        }

        private void OnRequestPhotoImage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string uri = AppViewModel.Instance.Properties.SelectedRequestItem.Photo;
            AppServices.Current.NavigationService.NavigateDirect(ViewsUris.OpenPhoto);
        }

        private void OnShareAppBarButton_Click(object sender, EventArgs e)
        {
            string uri = AppViewModel.Instance.Properties.SelectedRequestItem.Permalink;
          
            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.LinkUri = new Uri(uri, UriKind.Absolute);
            shareLinkTask.Message = AppViewModel.Instance.Properties.SelectedRequestItem.Description;

            shareLinkTask.Show();

            Tracking.SendSocial("WP8", "share", uri);

        }

        private void OnSupportAppBarButton_Click(object sender, EventArgs e)
        {
            this.ViewModel.ToggleRequestItemSupportCommand.Execute(null);
        }

        private void OnUploadsMenuItem_Click(object sender, EventArgs e)
        {
            this.ViewModel.UploadsCommand.Execute(null);
        }

        private void OnAboutMenuItem_Click(object sender, EventArgs e)
        {
            this.ViewModel.AboutCommand.Execute(null);
        }

        private void OnSignOutMenuItem_Click(object sender, EventArgs e)
        {
            this.ViewModel.SignOutCommand.Execute(null);
        }

        private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            MapsTask mapsTask = new MapsTask();
            //mapsTask.SearchTerm = "coffee";
            mapsTask.ZoomLevel = 10;

            object tag = (sender as Image).Tag;

            if(tag != null)
            {
                string[] parts = tag.ToString().Split(',');
                
                if(parts.Length == 2)
                {
                    mapsTask.Center = new GeoCoordinate(Convert.ToDouble(parts[0]), Convert.ToDouble(parts[1]));
                }
            }
            

            //Omit the Center property to use the user's current location.
            //mapsTask.Center = new GeoCoordinate(47.6204, -122.3493);

            

            mapsTask.Show();
        }
    }
}